package com.example.databindingdemo.viewmodel;

import android.util.Log;

import androidx.databinding.BaseObservable;
import androidx.databinding.Bindable;

import com.example.databindingdemo.BR;

/**
 * @author: njb
 * @date: 2022/9/18 16:27
 * @desc: 使用ViewModel实现双向绑定
 */
public class TwoWayBindingViewModel extends BaseObservable {
    private LoginModel loginModel;

    public TwoWayBindingViewModel(){
        loginModel = new LoginModel();
        loginModel.userName = "JetPack";
    }

    @Bindable
    public String getUserName(){
        return loginModel.userName;
    }

    public void setUserName(String userName){
        if(userName != null && !userName.equals(loginModel.userName)){
            loginModel.userName = userName;
            notifyPropertyChanged(BR.userName);
            Log.d("--viewModel--",userName);
        }
    }
}
